﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Globalization;
using System.Windows.Forms;
using VSTO = Microsoft.Office.Tools.Word;
//using Microsoft.Office.Tools.Word.Extensions;
using Microsoft.Office.Interop.SmartTag;
using Word = Microsoft.Office.Interop.Word;
using System.Diagnostics;
using System.ComponentModel;

namespace ContentProcessor
{
    public class OCISmartTag : VSTO.SmartTag
    {

        // Declare Actions for this SmartTag
        private VSTO.Action Action1 = new VSTO.Action("Open Content in Cloud");
        public Word.Document m_doc { get; set; } 
        private Dictionary<string, string> m_data; 

        public OCISmartTag(string smartTagType, string caption, Dictionary<string, string> smartTagData, Word.Document doc)
            : base(
                smartTagType,
                caption)
        {
            m_data = smartTagData; 
            for (int i = 0; i<smartTagData.Count; i++)
            {
                this.Terms.Add(smartTagData.ElementAt(i).Key); 
            }
            Actions = new VSTO.Action[] { Action1 };
            Action1.Click +=
                new VSTO.ActionClickEventHandler(Action1_Click);
            m_doc = doc;  
        }

        public void CleanUp()
        {
            this.Terms.Clear();
            this.m_data.Clear(); 
        }

        public void AddNewTerm(string term, string data)
        {
            m_data.Add(term, data);
            this.Terms.Add(term); 
        }
        
        private void Action1_Click(object sender, VSTO.ActionEventArgs e)
        {
            //e.Range.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdYellow;
           string link = e.Properties.get_ValueFromIndex(0);
           try
           {
               Process.Start(link);
           }
           catch (Win32Exception)
           {
               //nothing is registered to handle URLs, so let's use IE!
               Process.Start("IExplore.exe", link);
           } 
        }

        protected override void Recognize(string text,
            ISmartTagRecognizerSite site, ISmartTagTokenList tokenList)
        {
            foreach (string term in this.Terms)
            {
                // Search the text for the current smart tag term.
                int index = this.ParagraphText.IndexOf(term, 0);

                while (index >= 0)
                {
                    // Create a smart tag token and a property bag for the 
                    // recognized term.
                    ISmartTagProperties propertyBag =
                        site.GetNewPropertyBag();

                    // Write a new property value.
                    string key = "Key1";
                    propertyBag.Write(key, m_data[term]);
                    
                   
                    //
                    /*
                    string[] terms_word = term.Split(new char[] { ' ' });

                    
                    foreach (string term_word in terms_word)
                    {
                        foreach (Word.Range word in m_doc.Words)
                            //If we trim, we need to highlight less than the size of the word otherwise the space after
                            //the word is highlighted as well.
                            if (string.Compare(word.Text.Trim(), term_word, true, CultureInfo.InvariantCulture) == 0)
                            {
                                object oRef = word; 
                                //word.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText, ref object); 
                                word.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdYellow;
                                //word.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineDottedHeavy; 
                                //word.Bookmarks.Add("AAAA", ref MissingFieldException 
                                //Word.Bookmark bm = word.Application.ActiveDocument.Bookmarks.Add(term, ref oRef); 
                               
                            }
                    }
                    // Attach the smart tag to the term in th e document
                    
                     */
                    this.PersistTag(index, term.Length, propertyBag);
                    //VSTO.RichTextContentControl control = new VSTO.RichTextContentControl(
                    
                    // Increment the index and then find the next
                    // instance of the smart tag term.
                    index += term.Length;
                    index = ParagraphText.IndexOf(term, index);
                }
            }

        }

        
        
    }


}
